package com.lazyfish.bootcm.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lazyfish.bootcm.entity.User;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        boolean result = true;
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("USER_SESSION");//判断session中是否有用户数据，如果有，则返回true，继续向下执行
        if (user == null) {//不符合条件的给出提示信息，并转发到登录页面
            response.setContentType("text/json;charset=utf-8");
            response.sendRedirect(request.getContextPath()+"/login.html");
            result = false;
        }
        return result;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}